package cn.ty.comm.dict.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import cn.ty.comm.dict.dao.SysBizDictDao1;
import cn.ty.comm.dict.dto.SysBizQuertDTO;
import cn.ty.comm.dict.entity.SysBizDict;
import cn.ty.comm.dict.service.BizDictService;
import cn.ty.jpa.Specifications;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author summit
 * @since 2021/7/4 12:24
 */
@Service
@RequiredArgsConstructor
@Slf4j
public class BizDictServiceImpl implements BizDictService {

	private final SysBizDictDao1 sysBizDictDao1;

	@Override
	public void conditionSelect1() {
		SysBizQuertDTO dto = new SysBizQuertDTO();
		dto.setDictCode("222");
		Specification<SysBizDict> spec = Specifications.conditionOf(dto);
		Sort dictCode = Sort.by(Order.asc("dictCode"));
		List<SysBizDict> all = sysBizDictDao1.findAll(spec);
	}

	@Override
	public void conditionSelect() {
		Specification<SysBizDict> spec = Specifications.where(e -> {
			e.or(e1 -> {
				e1.eq("dictCode", "1").eq("dictName", "1");
			});
			e.eq("parentCode", "sys");
		});
		Sort dictCode = Sort.by(Order.asc("dictCode"));
		List<SysBizDict> all = sysBizDictDao1.findAll(spec);
//		PageImpl<SysBizDict> all1 = sysBizDictDao1.findAll(spec, PageRequest.of(0, 1, dictCode));
//		PageImpl impl = new PageImpl();
//		impl.
		log.info("data:{}", all);
	}


	@Override
	public void objectQuery() {
		SysBizQuertDTO dto = new SysBizQuertDTO();
		dto.setParentCode("1");
		dto.setParentDesc("2");
		dto.setDictCode("3");
		dto.setDictName("4");
		Specification<SysBizDict> spec = Specifications.conditionOf(dto);
		List<SysBizDict> all = sysBizDictDao1.findAll(spec);
		log.info("data:{}", all);
	}


	@Override
	public void batchSave() {

		List<SysBizDict> list = new ArrayList<>();

		for (int i = 0; i < 5000; i++) {
			SysBizDict dto = new SysBizDict();
			dto.setParentCode("3" + i);
			dto.setParentDesc("2");
			dto.setDictCode("3");
			dto.setDictName("4");
			list.add(dto);
		}
		;
		sysBizDictDao1.saveAll(list);

	}


	@PersistenceContext()
	protected EntityManager em;


	@Transactional
	@Override
	public void batchSave1() {
//		List<SysBizDict> list = new ArrayList<>();
//		SysBizDict dto = new SysBizDict();
//		dto.setParentCode("3");
//		dto.setParentDesc("2");
//		dto.setDictCode("3");
//		dto.setDictName("4");
//
//
//		SysBizDict dto1 = new SysBizDict();
//		dto1.setParentCode("4");
//		dto1.setParentDesc("2");
//		dto1.setDictCode("3");
//		dto1.setDictName("4");
//		list.add(dto);
//		list.add(dto1);

		List<SysBizDict> list = new ArrayList<>();

		for (int i = 0; i < 5000; i++) {
			SysBizDict dto = new SysBizDict();
			dto.setParentCode("3" + i);
			dto.setParentDesc("2");
			dto.setDictCode("3");
			dto.setDictName("4");
			em.persist(dto);
			list.add(dto);
		}
		;
	}

}
